<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Nhập hàng</title>
</head>

<body>
<?php
@session_start();
require('models/m_nhaphang.php');
require('models/sharefunction.php');

if (isset($_POST["themsach"])) {
    $id = $_POST["themsach"];
    if (isset($_SESSION['nhaphang']) && is_array($_SESSION['nhaphang'])) {
        $count = count($_SESSION['nhaphang']);
        $flag  = false;
        for ($i = 0; $i < $count; $i++) {
            if ($_SESSION['nhaphang'][$i]["ID_Sach"] == $_POST['ID_Sach']) {
                $_SESSION['nhaphang'][$i]["Gianhap"] = $_POST['Gianhap'];
                $_SESSION['nhaphang'][$i]["Soluong"] = $_POST['Soluong'];
                $flag                                = true;
                break;
            }
        }
        if ($flag == false) {
            $_SESSION['nhaphang'][$count]["ID_Sach"] = $_POST['ID_Sach'];
            $_SESSION['nhaphang'][$count]["Gianhap"] = $_POST['Gianhap'];
            $_SESSION['nhaphang'][$count]["Soluong"] = $_POST['Soluong'];
        }
        
    } else {
        $_SESSION['nhaphang']               = array();
        $_SESSION['nhaphang'][0]["ID_Sach"] = $_POST['ID_Sach'];
        $_SESSION['nhaphang'][0]["Gianhap"] = $_POST['Gianhap'];
        $_SESSION['nhaphang'][0]["Soluong"] = $_POST['Soluong'];
        
    }
    header("Location: nh.php");
    
}

dbconnect();


$ID_AI = getid_auto_increment();
$ncc   = getbyall_ncc();
$sach  = getbyall_sach();



if (isset($_POST['ok'])) {
    $tt = 0;
    if (isset($_SESSION['nhaphang'])) {
        for ($i = 0; $i < count($_SESSION['nhaphang']); $i++) {
            if ($_SESSION['nhaphang'][$i] == NULL)
                continue;
            $j         = $i + 1;
            $thanhtien = $_SESSION['nhaphang'][$i]["Gianhap"] * $_SESSION['nhaphang'][$i]["Soluong"];
            $query     = mysql_query("SELECT * FROM sach WHERE ID=" . $_SESSION['nhaphang'][$i]['ID_Sach']);
            $row       = mysql_fetch_array($query);
            $tt += $thanhtien;
        }
    }
    
    insert_hoadonnhap($_POST, $tt);
    
    $tt = 0;
    if (isset($_SESSION['nhaphang'])) {
        for ($i = 0; $i < count($_SESSION['nhaphang']); $i++) {
            if ($_SESSION['nhaphang'][$i] == NULL)
                continue;
            $j         = $i + 1;
            $thanhtien = $_SESSION['nhaphang'][$i]["Gianhap"] * $_SESSION['nhaphang'][$i]["Soluong"];
            $query     = mysql_query("SELECT * FROM sach WHERE ID=" . $_SESSION['nhaphang'][$i]['ID_Sach']);
			$row       = mysql_fetch_array($query);
			$soluong = $row["So_luong"] +  $_SESSION['nhaphang'][$i]["Soluong"];
			$insert_soluong =  mysql_query("UPDATE sach SET So_luong=N'".$soluong."' WHERE ID='".$_SESSION['nhaphang'][$i]['ID_Sach']."'");
            $maxid     = mysql_query("SELECT MAX(ID) as maxid FROM Hoa_don_nhap");
            $row2      = mysql_fetch_array($maxid);
            $idmax     = $row2["maxid"];
            
            $tt += $thanhtien;
            $ID_Sach = $_SESSION['nhaphang'][$i]["ID_Sach"];
            $Gianhap = $_SESSION['nhaphang'][$i]["Gianhap"];
            $Soluong = $_SESSION['nhaphang'][$i]["Soluong"];
            chi_tiet_nhap($ID_Sach, $Soluong, $idmax, $Gianhap, $thanhtien);
        }
			unset($_SESSION['nhaphang']);
    }
    
    $ID_AI = getid_auto_increment();
    header('Location: nh.php');
}


if (isset($_GET['xoaitem'])) {
    $item                        = $_GET['xoaitem'] - 1;
    $_SESSION['nhaphang'][$item] = NULL;
    
    header('location: nh.php');
    
}


if(isset($_POST['xoa']))
			{
			 
				unset($_SESSION['nhaphang']);
				 header('location: nh.php');

			}

require('v_nh.php');

?>
</body>
</html>